﻿/****************************** Module Header ******************************\
 * Module Name:  ExMethodForTabControl.cs
 * Module Version:  1.0.0 (2012-9-8)
 * Copyright (c) AnyEB.Com (14507247@qq.com)
 * 
 * This class is used for ExMethodForTabControl. 
 * 
 * This source is subject to the GPL License.
 * See http://www.gnu.org/licenses/gpl.html.
 * All other rights reserved.
 * 
 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
 * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
\***************************************************************************/
using System;
using System.Drawing;
using System.Windows.Forms;

namespace UCF.Core.Forms
{
    public static class ExMethodForTabControl
    {
        /// <summary>
        /// 双击关闭选项卡
        /// <para>2012-9-8</para>
        /// </summary>
        /// <param name="tc"></param>
        public static void OnDoubleClickCloseTab(this TabControl tc)
        {
            tc.MouseDoubleClick += (object sender, MouseEventArgs e) =>
            {
                TabControl tcMe = (TabControl)sender;
                for (int i = 0; i < tcMe.TabCount; i++)
                {
                    Rectangle recTab = tcMe.GetTabRect(i);
                    if (recTab.Contains(e.Location))
                    {
                        tcMe.Controls.Remove(tcMe.SelectedTab);
                        if (tcMe.SelectedIndex > 0) tcMe.SelectTab(tcMe.SelectedIndex - 1);
                        return;
                    }
                }
            };
        }

        /// <summary>
        /// 显示标签栏右键菜单
        /// <para>2012-9-8</para>
        /// </summary>
        /// <param name="tc"></param>
        /// <param name="cms"></param>
        public static void OnRightClickShowContextMenu(this TabControl tc, ContextMenuStrip cms)
        {
            tc.MouseUp += (object sender, MouseEventArgs e) =>
            {
                TabControl tcMe = (TabControl)sender;
                if (e.Button == MouseButtons.Right && e.Y < tcMe.GetTabRect(0).Bottom) cms.Show(tcMe, e.Location);
            };
        }

        /// <summary>
        /// 关闭其他选项卡
        /// <para>2012-9-8</para>
        /// </summary>
        /// <param name="tc"></param>
        public static void CloseOtherTabs(this TabControl tc)
        {
            foreach (TabPage tp in tc.TabPages)
            {
                if (tp != tc.SelectedTab) tp.Dispose();
            }
        }

        /// <summary>
        /// 关闭左侧选项卡
        /// <para>2012-9-8</para>
        /// </summary>
        /// <param name="tc"></param>
        public static void CloseLeftTabs(this TabControl tc)
        {
            for (int i = 0; i < tc.TabCount; i++)
            {
                if (i < tc.SelectedIndex) tc.TabPages[i].Dispose();
            }
        }

        /// <summary>
        /// 关闭右侧选项卡
        /// <para>2012-9-8</para>
        /// </summary>
        /// <param name="tc"></param>
        public static void CloseRightTabs(this TabControl tc)
        {
            for (int i = 0; i < tc.TabCount; i++)
            {
                if (i > tc.SelectedIndex) tc.TabPages[i].Dispose();
            }
        }

        #region 外观

        /// <summary>
        /// 设置标签背景颜色
        /// </summary>
        /// <param name="tc"></param>
        public static void SetTabBgColor(this TabControl tc, Color normalBg, Color selectedBg, Color fontColor)
        {
            tc.DrawMode = TabDrawMode.OwnerDrawFixed;
            tc.SizeMode = TabSizeMode.Normal;
            tc.DrawItem += (object s, DrawItemEventArgs e) =>
            {
                Rectangle tpRec = tc.GetTabRect(e.Index);
                StringFormat sf = new StringFormat();
                sf.Alignment = StringAlignment.Center;
                sf.LineAlignment = StringAlignment.Center;
                if (tc.SelectedIndex == e.Index)
                {
                    e.Graphics.FillRectangle(new SolidBrush(selectedBg), e.Bounds);
                }
                else
                {
                    e.Graphics.FillRectangle(new SolidBrush(normalBg), e.Bounds);
                }
                /*Point[] tx = new Point[]{
                    new Point(e.Bounds.X,e.Bounds.Y),
                    new Point(e.Bounds.X+e.Bounds.Width-8,e.Bounds.Y),
                    new Point(e.Bounds.X+e.Bounds.Width+8,e.Bounds.Y+e.Bounds.Height),
                    new Point(e.Bounds.X,e.Bounds.Y+e.Bounds.Height)
                };*/
                e.Graphics.DrawString(tc.TabPages[e.Index].Text, e.Font, new SolidBrush(fontColor), tpRec, sf);
            };
        }
        #endregion
    }
}
